package com.bakery.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bakery.common.Result;
import com.bakery.dto.BreadDTO;
import com.bakery.dto.StockChangeDTO;
import com.bakery.entity.Bread;
import com.bakery.entity.StockRecord;
import com.bakery.service.BreadService;
import com.bakery.service.StockRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 面包Controller
 *
 * @author bakery
 */
@RestController
@RequestMapping("/bread")
public class BreadController {
    
    @Autowired
    private BreadService breadService;
    
    @Autowired
    private StockRecordService stockRecordService;
    
    /**
     * 分页查询面包
     */
    @GetMapping("/page")
    public Result<Page<Bread>> getBreadPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer status) {
        Page<Bread> breadPage = breadService.getBreadPage(page, size, keyword, category, status);
        return Result.success(breadPage);
    }
    
    /**
     * 查询所有在售面包（用于下拉选择）
     */
    @GetMapping("/list")
    public Result<List<Bread>> getBreadList() {
        List<Bread> breadList = breadService.lambdaQuery()
                .eq(Bread::getStatus, 1)
                .orderByDesc(Bread::getCreateTime)
                .list();
        return Result.success(breadList);
    }
    
    /**
     * 获取面包详情
     */
    @GetMapping("/{id}")
    public Result<Bread> getBreadDetail(@PathVariable Long id) {
        Bread bread = breadService.getById(id);
        return Result.success(bread);
    }
    
    /**
     * 新增面包
     */
    @PostMapping
    public Result<Void> addBread(@Validated @RequestBody BreadDTO dto) {
        breadService.addBread(dto);
        return Result.success();
    }
    
    /**
     * 更新面包
     */
    @PutMapping
    public Result<Void> updateBread(@Validated @RequestBody BreadDTO dto) {
        breadService.updateBread(dto);
        return Result.success();
    }
    
    /**
     * 删除（下架）面包
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteBread(@PathVariable Long id) {
        breadService.deleteBread(id);
        return Result.success();
    }
    
    /**
     * 库存变动（入库/出库）
     */
    @PostMapping("/changeStock")
    public Result<Void> changeStock(@Validated @RequestBody StockChangeDTO dto) {
        breadService.changeStock(dto);
        return Result.success();
    }
    
    /**
     * 查询库存变动记录
     */
    @GetMapping("/stockRecord/page")
    public Result<Page<StockRecord>> getStockRecordPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long breadId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<StockRecord> recordPage = stockRecordService.getStockRecordPage(page, size, breadId, startTime, endTime);
        return Result.success(recordPage);
    }
}

